<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of profile
 *
 * @author Nguyen Thai Binh
 */
class profile extends CI_Controller {

  //put your code here
  public function __construct() {
    parent::__construct();
    set_userdata('end_user_current_url', current_url());
    if (!userdata('country_id') || !userdata('state_id') || !userdata('city_id')) {
      set_userdata('redirect_view', current_url());
      if (!userdata('end_user_user_id')) {
        redirect('welcome');
      } else {
        redirect('welcome/location');
      }
      return;
    }
  }

  public function index() {
    set_userdata('end_user_current_url', current_url());
    if (userdata('end_user_role') == 'END-USER' && userdata('end_user_id')) {
      $end_user = $this->end_user_model->get(userdata('end_user_id'));
      if ($end_user) {
        $user = $this->user_model->get($end_user['user_id']);
        if ($user) {
          $this->load->view('user/profile', array(
              'countries' => $this->country_model->get_all(),
              'title_html' => $this->lang->line('label.front-end.title.profile'),
              'obj' => array_merge($user, $end_user)));
          return;
        }
      }
    }
    redirect('');
  }

  public function update() {
    if (userdata('end_user_role') == 'END-USER' && userdata('end_user_id')) {
      $end_user = $this->end_user_model->get(userdata('end_user_id'));
      if ($end_user) {
        $user = $this->user_model->get($end_user['user_id']);
        if ($user) {
          $end_user['phone'] = isset($_REQUEST['phone']) ? $_REQUEST['phone'] : '';
          $user['display_name'] = isset($_REQUEST['display_name']) ? $_REQUEST['display_name'] : '';
          $end_user['address'] = isset($_REQUEST['address']) ? $_REQUEST['address'] : '';
          $end_user['country_id'] = isset($_REQUEST['country_id']) ? $_REQUEST['country_id'] : 0;
          $end_user['city_id'] = isset($_REQUEST['city_id']) ? $_REQUEST['city_id'] : 0;
          $end_user['state_id'] = isset($_REQUEST['state_id']) ? $_REQUEST['state_id'] : 0;
          $avatar = $this->base_lib->_upload_img('avatar', 'end_user');
          if (!empty($avatar)) {
            $end_user['avatar'] = $avatar;
          }
          $end_user['date_update'] = time();
          if (!empty($_REQUEST['password'])) {
            $user['password'] = md5($user['salt'] . md5($_REQUEST['password']));
          }
          $this->user_model->save($user);
          $this->end_user_model->save($end_user);
        }
      }
    }
    redirect('');
  }

  public function active($active_code = '') {
    $user = $this->user_model->get(array('active_code' => $active_code));
    if (!empty($user)) {
      $end_user = $this->end_user_model->get(array('user_id' => $user['id']));
      if (!empty($end_user)) {
        $end_user['date_active'] = time();
        $this->end_user_model->save($end_user);
        $user['is_active'] = TRUE;
        $user['active_code'] = '';
        $this->user_model->save($user);
        set_flash_message($this->lang->line('message.end_user.actived'));
        redirect('');
        return;
      }
    }
    set_flash_error($this->lang->line('error.active.code.not.found'));
    redirect('');
  }

}